import Vue from 'vue';
import VueRouter from 'vue-router';
import NProgress from 'nprogress'
import 'nprogress/nprogress.css';
// RouterTab 内置路由
import {RouterTabRoutes} from 'vue-router-tab'

import Layout from '../views/layout';
import manager from '../store/modules/manager';

Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
    //mode: 'history',
    routes: [
        {
            path: '/',
            redirect: '/dashboard',
            component: Layout,
            children: [
                ...RouterTabRoutes,
                {
                    path: 'dashboard',
                    name: 'dashboard',
                    component: () => import('../views/index'),
                    meta: {title: '后台面板', icon: 'dashboard'}
                }
            ],
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/login'),
        },
        {
            path: '/manager',
            component: Layout,
            meta: {title: '管理员管理', icon: 'user'},
            children: [
                {
                    path: '',
                    name: 'manager.index',
                    component: () => import('../views/manager/index'),
                    meta: {title: '管理员列表', icon: 'user'},
                },
                {
                    path: 'role-access',
                    name: 'manager.role-access',
                    component: () => import('../views/manager/role-access'),
                    meta: {title: '权限列表', icon: 'user'},
                },
                {
                    path: 'role-access/create',
                    name: 'manager.role-access.create',
                    component: () => import('../views/manager/role-access/create'),
                    meta: {title: '权限编辑', icon: 'user'},
                },
                {
                    path: 'role',
                    name: 'manager.role',
                    component: () => import('../views/manager/role/index'),
                    meta: {title: '角色列表', icon: 'user'},
                },
                {
                    path: 'role/create',
                    name: 'manager.role.create',
                    component: () => import('../views/manager/role/create'),
                    meta: {title: '添加角色', icon: 'user'},
                },
                {
                    path: 'role/:id(\\d+)',
                    name: 'manager.role.edit',
                    component: () => import('../views/manager/role/create'),
                    meta: {title: '编辑角色', icon: 'user'},
                },
                {
                    path: 'logs',
                    name: 'manager.logs',
                    component: () => import('../views/manager/logs'),
                    meta: {title: '操作日志', icon: 'user'},
                },
                {
                    path: 'one-click-login',
                    name: 'manager.one-click-login',
                    component: () => import('../views/manager/one-click-login'),
                    meta: {title: '授权登录', icon: 'user'},
                },
            ]
        },
        {
            path: '/menu',
            component: Layout,
            meta: {title: '菜单管理', icon: 'menu'},
            children: [
                {
                    path: 'backend',
                    name: 'menu.backend',
                    component: () => import('../views/menu/backend'),
                    meta: {title: '后台菜单', icon: 'menu'},
                },
            ]
        },
        {
            path: '/spread-data',
            component: Layout,
            meta: {title: '推广数据', icon: 'user'},
            children: [
                {
                    path: 'day-stat',
                    name: 'spreadData.dayStat',
                    component: () => import('../views/spread-data/day-stat'),
                    meta: {title: '整体数据报表', icon: 'user'},
                },
                {
                    path: 'source-stat',
                    name: 'spreadData.sourceStat',
                    component: () => import('../views/spread-data/source-stat'),
                    meta: {title: '渠道分析表', icon: 'user'},
                },
                {
                    path: 'daily',
                    name: 'spreadData.daily',
                    component: () => import('../views/spread-data/daily'),
                    meta: {title: '日报详表', icon: 'user'},
                },
                {
                    path: 'daily-agent',
                    name: 'spreadData.dailyAgent',
                    component: () => import('../views/spread-data/daily-agent'),
                    meta: {title: '日报详表A', icon: 'user'},
                },
                {
                    path: 'daily-agent-sub',
                    name: 'spreadData.dailyAgentSub',
                    component: () => import('../views/spread-data/daily-agent-sub'),
                    meta: {title: '日报详表AS', icon: 'user'},
                },
                {
                    path: 'daily-all',
                    name: 'spreadData.dailyAll',
                    component: () => import('../views/spread-data/daily-all'),
                    meta: {title: '日报总表', icon: 'user'},
                },
                {
                    path: 'month',
                    name: 'spreadData.month',
                    component: () => import('../views/spread-data/month'),
                    meta: {title: '月报详表', icon: 'user'},
                },
                {
                    path: 'month-all',
                    name: 'spreadData.monthAll',
                    component: () => import('../views/spread-data/month-all'),
                    meta: {title: '月报总表', icon: 'user'},
                },
                {
                    path: 'source-ltv',
                    name: 'spreadData.sourceLtv',
                    component: () => import('../views/spread-data/source-ltv'),
                    meta: {title: '渠道LTV', icon: 'user'},
                },
                {
                    path: 'hour',
                    name: 'spreadData.hour',
                    component: () => import('../views/spread-data/hour'),
                    meta: {title: '经营时段报表', icon: 'user'},
                },
                {
                    path: 'hour-simple',
                    name: 'old.spreadData.hourSimple',
                    component: () => import('../views/spread-data/hour-simple'),
                    meta: {title: '经营时段报表', icon: 'user'},
                },
                {
                    path: 'operate-total',
                    name: 'spreadData.operateTotal',
                    component: () => import('../views/spread-data/operate-total'),
                    meta: {title: '经营汇总表', icon: 'user'},
                },
                {
                    path: 'user-keep',
                    name: 'spreadData.userKeep',
                    component: () => import('../views/spread-data/user-keep'),
                    meta: {title: '留存率', icon: 'user'},
                },
                {
                    path: 'market-report-stat',
                    name: 'spreadData.marketReportStat',
                    component: () => import('../views/spread-data/market-report-stat'),
                    meta: {title: '市场上报统计', icon: 'user'},
                },
            ]
        },
        {
            path: '/setting',
            component: Layout,
            meta: {title: '系统设置', icon: 'tag'},
            children: [
                {
                    path: '',
                    name: 'setting.index',
                    component: () => import('../views/setting/index'),
                    meta: {title: '基础设置', icon: 'tag'},
                },
                {
                    path: 'horizon',
                    name: 'setting.horizon',
                    component: () => import('../views/setting/horizon'),
                    meta: {title: 'horizon', icon: 'tag'},
                },
            ]
        },
        {
            path: '/app-source',
            component: Layout,
            meta: {title: '渠道管理', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'appSource.index',
                    component: () => import('../views/app-source/index'),
                    meta: {title: '渠道列表', icon: 'user'},
                },
                {
                    path: 'config',
                    name: 'appSource.config',
                    component: () => import('../views/app-source/config'),
                    meta: {title: '渠道方案', icon: 'user'},
                },
            ]
        },
        {
            path: '/client-config',
            component: Layout,
            meta: {title: '客户端配置', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'clientConfig.index',
                    component: () => import('../views/client-config'),
                    meta: {title: '客户端配置', icon: 'user'},
                },
                {
                    path: 'create',
                    name: 'clientConfig.create',
                    component: () => import('../views/client-config/create'),
                    meta: {title: '客户端配置添加', icon: 'user'},
                },
                {
                    path: ':id(\\d+)',
                    name: 'clientConfig.edit',
                    component: () => import('../views/client-config/create'),
                    meta: {title: '客户端配置编辑', icon: 'user'},
                },
            ]
        },
        {
            path: '/app-resource',
            component: Layout,
            meta: {title: '应用资源', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'appReource.index',
                    component: () => import('../views/app-resource/index'),
                    meta: {title: '应用资源', icon: 'user'},
                },
            ]
        },
        {
            path: '/apk',
            component: Layout,
            meta: {title: '应用管理', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'apk.index',
                    component: () => import('../views/apk/index'),
                    meta: {title: '应用管理', icon: 'user'},
                },
            ]
        },
        {
            path: '/app-version',
            component: Layout,
            meta: {title: '版本管理', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'appVersion.index',
                    component: () => import('../views/app-version/index'),
                    meta: {title: '版本列表', icon: 'user'},
                },
            ]
        },
        {
            path: '/app-source-cpa',
            component: Layout,
            meta: {title: '渠道推广管理', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'appSourceCpa.index',
                    component: () => import('../views/app-source-cpa/tabs'),
                    meta: {title: '渠道推广管理', icon: 'user'},
                },
            ]
        },
        {
            path: '/announce',
            component: Layout,
            meta: {title: '系统公告', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'announce.index',
                    component: () => import('../views/announce'),
                    meta: {title: '系统公告', icon: 'user'},
                },
            ]
        },
        {
            path: '/app-news',
            component: Layout,
            meta: {title: 'APP大事件', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'app-news.index',
                    component: () => import('../views/app-news'),
                    meta: {title: 'APP大事件', icon: 'user'},
                },
            ]
        },
        {
            path: '/app-appid',
            component: Layout,
            meta: {title: '应用APPID', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'appAppid.index',
                    component: () => import('../views/app-appid'),
                    meta: {title: '应用APPID', icon: 'user'},
                },
            ]
        },
        {
            path: '/app-webpage',
            component: Layout,
            meta: {title: '应用网页', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'appWebpage.index',
                    component: () => import('../views/app-webpage'),
                    meta: {title: '应用网页', icon: 'user'},
                },
            ]
        },
        {
            path: '/sms-sign',
            component: Layout,
            meta: {title: '短信签名', icon: 'menu'},
            children: [
                {
                    path: '',
                    name: 'smsSign.index',
                    component: () => import('../views/sms-sign'),
                    meta: {title: '短信签名', icon: 'user'},
                },
            ]
        },
        {
            path: "*",
            component: Layout,
            children: [
                {
                    path: '',
                    component: () => import('../views/404'),
                    meta: {title: '404', icon: 'user'},
                }
            ]
        }
    ]
});

router.beforeEach((to, from, next) => {
    NProgress.start();

    if (to.name === 'sso-login') {
        next()
        return;
    }

    if (to.name !== 'login') {
        if (!manager.state.info || !manager.state.info.id) {
            // 前往登录页并记录当前访问URL
            next({
                name: 'login',
                query: {
                    redirect: to.fullPath,
                }
            });
            return
        }
    }
    next();
});

router.afterEach(() => {
    NProgress.done(true);
})

export default router;
